<?php

/*

  Action	$4

  +1 Action
  Each player may reveal a Province from his hand.
  If you do, discard it and gain a Prize (from the Prize pile) or a Duchy, putting it on top of your deck.
  If no-one else does, +1 Card, +$1.

 */

class Tournament extends Card implements iAction, iKingdom, iCornucopia {

    public function __construct() {
        parent::__construct("Tournament", 4);
        $this->undoPlay = false;
        $this->undoBuy = true;
    }

    public function getDescription() {
        return array(
            "+1 Action",
            "Each player may reveal a Province from his hand.  If you do, discard it and gain a Prize (from the Prize pile) or a Duchy, putting it on top of your deck.  If no-one else does, +1 Card, +$1.");
    }
    
    public function play(Game $game, $isRealCard) {
        $game->addActions(1);
         ScreenDisplay::addScreenMessage($game, true, "get +1 action", " gets +1 action");
        
        $names = $game->getOtherPlayersNames();
        foreach ($names as $name) {
            $otherProvince = $game->getCardInHand("Province", $name);
            if ($otherProvince) {
                $options = array(new Option(1, "Yes"), new Option(2, "No"));
                $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Reveal a province", $name, true);
            }
        }


        $province = $game->getCardInHand("Province");
        if ($province != null) {
            $options = array(new Option(1, "Yes"), new Option(2, "No"));
            $output = MySocket::GetUserReply($game, Input_ChooseOne, $options, 1, 1, null, "Reveal a province?");
            $answer = $output[0];
            if ($answer == "Yes") {
                $game->moveCardFromPileToPile($province, PlayHand, DiscardPile);
                $prizes = $game->getPrizes();
                
                if (sizeof($prizes) > 1) {
                    $output = MySocket::GetUserReply($game, Input_ChooseOne, $prizes, 1, 1, null, "Choose your prize");
                    $card_name = $output[0];
					$duchy = new Duchy();
					$card = null;
					if ($card_name == $duchy->getValue())
						$card = $game->moveCardFromPileToPile($card_name, BoardCards, DrawPile);
					else
						$card = $game->moveCardFromPileToPile($card_name, Prizes, DrawPile);
                     ScreenDisplay::addScreenMessage($game, true, "discard  ".  nicePrint($province)." and gain " . nicePrint($card), "discards ".  nicePrint($province)." and gains " . nicePrint($card));
                }
                else if (sizeof($prizes) == 1) {
                    $card = $game->moveCardFromPileToPile($prizes[0], Prizes, DrawPile);
                     ScreenDisplay::addScreenMessage($game, true, "discard ".  nicePrint($province)." and gain " . nicePrint($card), "discards ".  nicePrint($province)." and gains " . nicePrint($card));
                }
                else {
                     ScreenDisplay::addScreenMessage($game, true, "discard ".  nicePrint($province)." and gain nothing", "discards a ".  nicePrint($province)." and gains nothing");
                }
            }
        }
        $game->setPhase(Phase_Sleep);
        
        //the waiting function
        $users_replies = MySocket::getUsersReplies($game);
        $someone_revealed_province = false;
        $province = new Province();
        foreach ($users_replies as $name => $output) {
            $answer = $output[0];
            if ($answer == "Yes") {
                $someone_revealed_province = true;
                
                 ScreenDisplay::addScreenMessage($game, true, "reveal ".  nicePrint($province),"reveals ".  nicePrint($province), $name);
            }
        }

        if (!$someone_revealed_province) {
            $drawed_cards = $game->drawCards(1);
            $game->addMoney(new Price(1));
             ScreenDisplay::addScreenMessage($game, true, "get +1$ and draw " . nicePrint($drawed_cards). " since no one else revealed a Province", 
                    "gets +1$ and draws " . sizeof($drawed_cards). " cards since no one else revealed a Province");
        }

        $game->cardActionFinished();
        return true;
    }
    
    public function boardSetup(Game $game) {
        $game->addPrizes();
        return true;
    }

}

?>